<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="案卷号" prop="fileCode">
        <el-input
          v-model="queryParams.fileCode"
          placeholder="请输入案卷号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="档案类别" prop="classificationCode">
        <el-input
          v-model="queryParams.classificationCode"
          placeholder="请输入档案类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归档时间" prop="archiveTime">
        <el-date-picker clearable
          v-model="queryParams.archiveTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择归档时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="档案级别" prop="groupId">
        <el-input
          v-model="queryParams.groupId"
          placeholder="请输入档案级别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提名" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入提名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主题词" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入主题词"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="档案柜" prop="cabinetId">
        <el-input
          v-model="queryParams.cabinetId"
          placeholder="请输入档案柜"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="全宗" prop="fondCode">
        <el-input
          v-model="queryParams.fondCode"
          placeholder="请输入全宗"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="保存期限" prop="saveDate">
        <el-input
          v-model="queryParams.saveDate"
          placeholder="请输入保存期限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发文字号" prop="dispatchNumber">
        <el-input
          v-model="queryParams.dispatchNumber"
          placeholder="请输入发文字号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="立卷时间" prop="filingTime">
        <el-input
          v-model="queryParams.filingTime"
          placeholder="请输入立卷时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="立卷人" prop="filingPerson">
        <el-input
          v-model="queryParams.filingPerson"
          placeholder="请输入立卷人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="案卷立案开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择案卷立案开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="案卷立案结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择案卷立案结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="扩展索引" prop="extendedIndex">
        <el-input
          v-model="queryParams.extendedIndex"
          placeholder="请输入扩展索引"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['am:description:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['am:description:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['am:description:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['am:description:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="descriptionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="案卷号" align="center" prop="fileCode" />
      <el-table-column label="档案类别" align="center" prop="classificationCode" />
      <el-table-column label="归档时间" align="center" prop="archiveTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.archiveTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="档案级别" align="center" prop="groupId" />
      <el-table-column label="提名" align="center" prop="title" />
      <el-table-column label="主题词" align="center" prop="keyword" />
      <el-table-column label="备注" align="center" prop="notes" />
      <el-table-column label="档案柜" align="center" prop="cabinetId" />
      <el-table-column label="全宗" align="center" prop="fondCode" />
      <el-table-column label="保存期限" align="center" prop="saveDate" />
      <el-table-column label="发文字号" align="center" prop="dispatchNumber" />
      <el-table-column label="立卷时间" align="center" prop="filingTime" />
      <el-table-column label="立卷人" align="center" prop="filingPerson" />
      <el-table-column label="案卷立案开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="案卷立案结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="扩展索引" align="center" prop="extendedIndex" />
      <el-table-column label="借阅状态。0：正常。1：被借走。" align="center" prop="brStatus" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['am:description:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['am:description:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改档案著录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="案卷号" prop="fileCode">
          <el-input v-model="form.fileCode" placeholder="请输入案卷号" />
        </el-form-item>
        <el-form-item label="档案类别" prop="classificationCode">
          <el-input v-model="form.classificationCode" placeholder="请输入档案类别" />
        </el-form-item>
        <el-form-item label="归档时间" prop="archiveTime">
          <el-date-picker clearable
            v-model="form.archiveTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择归档时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="档案级别" prop="groupId">
          <el-input v-model="form.groupId" placeholder="请输入档案级别" />
        </el-form-item>
        <el-form-item label="提名" prop="title">
          <el-input v-model="form.title" placeholder="请输入提名" />
        </el-form-item>
        <el-form-item label="主题词" prop="keyword">
          <el-input v-model="form.keyword" placeholder="请输入主题词" />
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="档案柜" prop="cabinetId">
          <el-input v-model="form.cabinetId" placeholder="请输入档案柜" />
        </el-form-item>
        <el-form-item label="全宗" prop="fondCode">
          <el-input v-model="form.fondCode" placeholder="请输入全宗" />
        </el-form-item>
        <el-form-item label="保存期限" prop="saveDate">
          <el-input v-model="form.saveDate" placeholder="请输入保存期限" />
        </el-form-item>
        <el-form-item label="发文字号" prop="dispatchNumber">
          <el-input v-model="form.dispatchNumber" placeholder="请输入发文字号" />
        </el-form-item>
        <el-form-item label="立卷时间" prop="filingTime">
          <el-input v-model="form.filingTime" placeholder="请输入立卷时间" />
        </el-form-item>
        <el-form-item label="立卷人" prop="filingPerson">
          <el-input v-model="form.filingPerson" placeholder="请输入立卷人" />
        </el-form-item>
        <el-form-item label="案卷立案开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择案卷立案开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="案卷立案结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择案卷立案结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="扩展索引" prop="extendedIndex">
          <el-input v-model="form.extendedIndex" placeholder="请输入扩展索引" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDescription, getDescription, delDescription, addDescription, updateDescription } from "@/api/am/description";

export default {
  name: "Description",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 档案著录表格数据
      descriptionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileCode: null,
        classificationCode: null,
        archiveTime: null,
        groupId: null,
        title: null,
        keyword: null,
        notes: null,
        cabinetId: null,
        fondCode: null,
        saveDate: null,
        dispatchNumber: null,
        filingTime: null,
        filingPerson: null,
        startTime: null,
        endTime: null,
        extendedIndex: null,
        brStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fileCode: [
          { required: true, message: "案卷号不能为空", trigger: "blur" }
        ],
        classificationCode: [
          { required: true, message: "档案类别不能为空", trigger: "blur" }
        ],
        archiveTime: [
          { required: true, message: "归档时间不能为空", trigger: "blur" }
        ],
        groupId: [
          { required: true, message: "档案级别不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "提名不能为空", trigger: "blur" }
        ],
        cabinetId: [
          { required: true, message: "档案柜不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询档案著录列表 */
    getList() {
      this.loading = true;
      listDescription(this.queryParams).then(response => {
        this.descriptionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fileCode: null,
        classificationCode: null,
        archiveTime: null,
        groupId: null,
        title: null,
        keyword: null,
        notes: null,
        cabinetId: null,
        fondCode: null,
        saveDate: null,
        dispatchNumber: null,
        filingTime: null,
        filingPerson: null,
        startTime: null,
        endTime: null,
        extendedIndex: null,
        brStatus: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加档案著录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDescription(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改档案著录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDescription(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDescription(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除档案著录编号为"' + ids + '"的数据项？').then(function() {
        return delDescription(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('am/description/export', {
        ...this.queryParams
      }, `description_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
